package Pet.Management.repository;

import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import Pet.Management.domain.entity.Favorite;
import Pet.Management.domain.entity.Post;

@Repository
public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
    
    /**
     * 查询用户是否收藏了帖子
     */
    @Query("SELECT f FROM Favorite f WHERE f.user.id = :userId AND f.post.id = :postId")
    Optional<Favorite> findByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 统计帖子的收藏数
     */
    @Query("SELECT COUNT(f) FROM Favorite f WHERE f.post.id = :postId")
    Long countByPostId(@Param("postId") Long postId);
    
    /**
     * 查询用户收藏的帖子
     */
    @Query("SELECT f.post FROM Favorite f WHERE f.user.id = :userId ORDER BY f.createTime DESC")
    Page<Post> findFavoritePostsByUserId(@Param("userId") Long userId, Pageable pageable);
} 